package cn.edu.ecut.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@WebServlet( "/request/param" )
public class RequestParamServlet extends HttpServlet {

    @Override
    protected void service( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        request.setCharacterEncoding( "UTF-8" );
        response.setCharacterEncoding( "UTF-8" );

        /*
        BufferedReader br = request.getReader(); // 以字符流形式返回
        String s ;
        while( ( s = br.readLine() ) != null ){
            System.out.println( s );
        }
        System.out.println( "~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~" );
        */

        String username = request.getParameter( "username" );
        System.out.println( "username : " + username );

        String password = request.getParameter( "password" );
        System.out.println( "password : " + password );

        String[] hobby = request.getParameterValues( "hobby" );
        System.out.println( "hobby : " + Arrays.toString( hobby ) );

        Map<String,String[]> paramMap = request.getParameterMap(); // 获取所有的请求参数组成的Map集合
        System.out.println( "ParameterMap : " + paramMap );
    }
}
